/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package traffic.light.simulator.town.cars;

import java.awt.Color;
import java.awt.Graphics;
import java.util.Timer;
import java.util.TimerTask;
import traffic.light.simulator.town.Town;
import traffic.light.simulator.town.trafficLight.TrafficLight;
import traffic.light.simulator.town.unity.RoadElement;
import traffic.light.simulator.util.Position;

/**
 *
 * Class representing the Ambulance child of Car
 */
public class Ambulance extends Car {

    //timer to help for the flash lights
    private Timer timer;
    //the delay of flash lights
    private static int DELAY = 50;
    private boolean flashLights = true;;

    /**
     * Constructor of Ambulance
     * @param town the town
     */
    public Ambulance(Town town) {
        super(Color.WHITE, town);
        timer = new Timer();
        timer.schedule(new ActionFlashLights(this), 0, 500);
    }

    @Override
    public void update() {
        RoadElement road = town.getRoadElementAt(centerOfCar());
        if (road != null) {
            RoadElement r2 = town.getRoadElementAt(new Position(pos.getX() + SIZE - 2, pos.getY()));
            RoadElement r3 = town.getRoadElementAt(new Position(pos.getX(), pos.getY() + SIZE - 2));
            RoadElement r4 = town.getRoadElementAt(new Position(pos.getX() + SIZE - 2, pos.getY() + SIZE - 2));
            boolean greenTrafficLight = true;
            TrafficLight tl = road.getTL();

            if (tl != null) {
                greenTrafficLight = tl.getTLValueFromPosition(road.getAbsX(), road.getAbsY());
            }

            if (road == r2
                    && r2 == r3
                    && r3 == r4) {
                updateDirection();
            }
            
            currentRoad = road;
            if (!greenTrafficLight) {
                tl.change();
            }
            move();
        }
    }
    
    /**
     * Change the flash lights of the Ambulance
     */
    public void changeFlashLights() {
        flashLights = !flashLights;
    }

    /**
     *
     * @param p
     */
    @Override
    public void setPosition(Position p) {
        this.pos = p;
        currentRoad = town.getRoadElementAt(pos);
    }

    @Override
    public void draw(Graphics g) {
        g.setColor(clr);
        g.fillRect(pos.getX(), pos.getY(), SIZE, SIZE);
        g.setColor(Color.RED);
        if(flashLights) {
            int flashLightSize = SIZE/4;
            g.fillRect(centerOfCar().getX() - flashLightSize/2,
                    centerOfCar().getY() - flashLightSize/2,
                    flashLightSize,
                    flashLightSize);
        }
    }

    /**
     * ActionFlashLights is a class to help for changing the flash lights
     */
    class ActionFlashLights extends TimerTask {
        private Ambulance a;
        
        public ActionFlashLights(Ambulance a) {
            this.a = a;
        }

        public void run() {
            a.changeFlashLights();
        }
    }
}
